
package com.kugou.game.sdk.base;

import com.kugou.game.sdk.core.SDKActions;
import com.kugou.game.sdk.ui.activity.CommonSdkActivity;
import com.kugou.game.sdk.ui.dialog.BaseConfirmDialog;
import com.kugou.game.sdk.ui.fragment.FAQListFragment;
import com.kugou.game.sdk.ui.widget.HelpMenuPopupWindow;
import com.kugou.game.sdk.ui.widget.VipPopupWindow;
import com.kugou.game.sdk.utils.Rx;
import com.kugou.game.sdk.utils.ToastUtil;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.text.ClipboardManager;
import android.text.Spanned;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;

public class BaseCommonTitleFragmentActivity extends BaseSDKWorkerFragmentActivity {

    /**
     * 常用参数传递key
     */
    public static final String UNIXTIME = "unixtime";

    public static final String TOKEN = "token";

    public static final String TITLE_KEY = "title_key";

    public static final String USER_NICKNAME_KEY = "user_nickname_key";

    public static final String USER_PASSWORD_KEY = "user_password_key";

    public static final String USER = "user";

    public static final String VALIDATE_CODE = "validate_code";

    public static final String PHONE = "phone";

    public static final String CMD = "cmd";

    public static final String PASSWORD = "password";

    /**
     * 获取验证码
     */
    protected static final int REQUEST_VALIDATE_CODE = 100;

    /**
     * 消息Item id
     */
    public static final String MSG_ID = "msg_id";

    /**
     * 消息Item角标
     */
    public static final String MSG_POSITION = "msg_position";

    /**
     * 跳转来自于哪个页面
     */
    public static final String ACTIVITY_INDEX_KEY = "activity_index_key";

    /**
     * 跳转来自于某个功能或页面点击动作
     */
    public static final String FROM_ACTION_KEY = "from_key";

    /**
     * 来自于正常情况
     */
    public static final int FROM_NORMAL = 1;

    /**
     * 来自于充值入口
     */
    public static final int FROM_RECHARGE = 2;

    /**
     * 来自于切换账号
     */
    public static final int FROM_CHANGE_ACCOUNT = 3;

    /**
     * 来自于进入游戏或登录之前进入登录
     */
    public static final int FROM_BEFORE_ENTER_GAME_LOGIN = 4;

    /**
     * 来自于进入游戏或登录游戏之后进入登录
     */
    public static final int FROM_AFTER_ENTER_GAME_LOGIN = 5;

    /**
     * 来自于进入游戏或登录之前进入注册
     */
    public static final int FROM_BEFORE_ENTER_GAME_REGISTER = 6;

    /**
     * 来自于进入游戏或登录游戏之后进入注册
     */
    public static final int FROM_AFTER_ENTER_GAME_REGISTER = 7;

    /**
     * 来自于主界面的游戏论坛入口
     */
    public static final int FROM_FORUM = 8;

    /**
     * 来自于礼包页面进入注册
     */
    public static final int FROM_GIFTBAG_REGISTER = 9;

    /**
     * 来自于巨炮游戏中心
     */
    public static final int FROM_GAME_CENTER = 10;

    protected Context mContext;

    /**
     * 标题栏
     */
    public RelativeLayout mTitleBar;

    /**
     * 标题栏的回退键
     */
    private ImageView mBackImg;

    /**
     * 标题
     */
    private TextView mTitle;

    /**
     * 标题栏右边的按钮,帮助按钮
     */
    protected ImageView mCommonRightImg, mHelpImage, mVipImage;

    /**
     * 标题栏下边的支付金额
     */
    protected TextView mCommonTipsBarMoneyNum;

    /**
     * 标题栏下使用游币充值文本
     */
    protected TextView mPayByKubiTextView, mGSTextView;

    // 客服电话、客服QQ、充值提示
    private BaseConfirmDialog mConfirmDialog, mQQDialog, mRechargeTips;

    private HelpMenuPopupWindow mHelpWindow;

    private VipPopupWindow mVipWindow;

    @Override
    public void onContentChanged() {
        super.onContentChanged();
        mContext = this;
        int titleBarId = Rx.id.kg_common_title_bar;
        mTitleBar = (RelativeLayout) findViewById(titleBarId);
        int backImgId = Rx.id.kg_layer_back_image;
        mBackImg = (ImageView) findViewById(backImgId);
        int titleId = Rx.id.kg_layer_title_text;
        mTitle = (TextView) findViewById(titleId);
        int rightId = Rx.id.kg_layer_right_image;
        mCommonRightImg = (ImageView) findViewById(rightId);
        int helpId = Rx.id.kg_layer_help_image;
        mHelpImage = (ImageView) findViewById(helpId);

        mVipImage = (ImageView) findViewById(Rx.id.kg_layer_vip_image);

        mGSTextView = (TextView) findViewById(Rx.id.kg_layer_gs_image);

        if (mBackImg == null) {
            throw new IllegalArgumentException("can not find R.id.kg_layer_back_image.");
        }
        if (mTitle == null) {
            throw new IllegalArgumentException("can not find R.id.kg_layer_title_text.");
        }
        if (mCommonRightImg == null) {
            throw new IllegalArgumentException("can not find R.id.kg_layer_right_image.");
        }

        if (mVipImage == null) {
            throw new IllegalArgumentException("can not find R.id.kg_layer_vip_image.");
        }

        mBackImg.setOnClickListener(mBackBtnClickListener);

        mGSTextView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                onShowGSImageListener.onShow();
            }
        });

        // 客服电话对话框
        mConfirmDialog = createPhoneDialog();

        // qq服务
        mQQDialog = createQQNumDialog();

        // 充值提示
        mRechargeTips = createRechargeTips();

        // 帮助菜单
        mHelpWindow = createHelpMenuPopupWindow(mContext);
        mHelpImage.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                mHelpWindow.show(v);
            }
        });

    }

    @Override
    public void setupActions(ArrayList<String> actions) {
        super.setupActions(actions);
        actions.add(SDKActions.ACTION_CHANGE_TO_VIP);
    }

    @Override
    public void handleBroadcast(Context context, Intent intent) {
        super.handleBroadcast(context, intent);
        String action = intent.getAction();
        if (SDKActions.ACTION_CHANGE_TO_VIP.equals(action) && mVipWindow != null) {
            mVipWindow.updateBtnText();
        }
    }

    private BaseConfirmDialog createRechargeTips() {
        final BaseConfirmDialog dialog = new BaseConfirmDialog(this);
        dialog.setDialogTitle("充值提示");
        dialog.setCancelBtnVisibility(View.GONE);
        dialog.setOKBtnText("知道了");
        dialog.setOkBtnBackground(Rx.drawable.kg_selector_bottom_single_filleted_corner_retangle);
        dialog.setOKClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        return dialog;
    }

    private BaseConfirmDialog createQQNumDialog() {
        final BaseConfirmDialog dialog = new BaseConfirmDialog(this);
        dialog.setDialogTitle("提示");
        dialog.setMessage(getString(Rx.string.kg_custom_service_qq));
        dialog.setTextGravity(Gravity.CENTER);
        dialog.setOKBtnText("复制QQ号");
        dialog.setOKClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
                ClipboardManager cmb = (ClipboardManager) mContext
                        .getSystemService(Context.CLIPBOARD_SERVICE);
                cmb.setText(getString(Rx.string.kg_custom_service_qqnum));
                ToastUtil.showMsg("QQ号已复制");
            }
        });
        return dialog;
    }

    private BaseConfirmDialog createPhoneDialog() {
        final BaseConfirmDialog dialog = new BaseConfirmDialog(this);
        final String servicePhone = mContext.getResources().getString(
                Rx.string.kg_custom_service_phone);
        dialog.setDialogTitle("提示");
        dialog.setMessage("7x24小时客服热线电话：" + servicePhone);
        dialog.setTextGravity(Gravity.CENTER);
        dialog.setOKBtnText("拨打");
        dialog.setOKClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
                try {
                    Intent intent = new Intent(Intent.ACTION_DIAL);
                    Uri uri = Uri.parse("tel:" + servicePhone);
                    intent.setData(uri);
                    mContext.startActivity(intent);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        return dialog;
    }

    protected void onTipsDialogShow(BaseConfirmDialog dialog, int position) {
    }

    /**
     * 若需要自定义菜单列表的样式，可重写次方法创建自定义样式的popupWindow
     * 
     * @param context
     * @return
     */
    protected HelpMenuPopupWindow createHelpMenuPopupWindow(Context context) {
        HelpMenuPopupWindow hWindow = new HelpMenuPopupWindow(context, initMenuItemNums(),
                new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                        mHelpWindow.dismiss();
                        onMenuItemClick(arg0, arg1, arg2, arg3);
                    }
                });
        return hWindow;
    }

    /**
     * 子类重写此方法来确定帮助按钮有几个item
     */
    protected int initMenuItemNums() {
        return HelpMenuPopupWindow.HELP_MENU_ITEM_COUNT_THREE;
    }

    /**
     * 帮助按钮里菜单的点击事件回调
     */
    protected void onMenuItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (position == 0) {
            showPhoneDialog();
        } else if (position == 1) {
            showQQDialog();
        } else if (position == 2) {
            mContext.startActivity(CommonSdkActivity.creatIntent(mContext,
                    FAQListFragment.class.getName(), Rx.string.kg_custom_service_faqpage));
            // help.setVisibility(View.GONE);
        } else {
            onTipsDialogShow(mRechargeTips, position);
            mRechargeTips.show();
        }
    }

    protected void showPhoneDialog() {
        onTipsDialogShow(mConfirmDialog, 0);
        mConfirmDialog.show();
    }

    protected void showQQDialog() {
        onTipsDialogShow(mQQDialog, 1);
        mQQDialog.show();
    }

    protected void showVipIcon() {
        mVipImage.setVisibility(View.VISIBLE);

        mVipWindow = new VipPopupWindow(mContext);

        mVipImage.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mVipWindow != null) {
                    mVipWindow.show(v);
                }
            }
        });
    }

    public void showGSIcon() {
        mGSTextView.setVisibility(View.VISIBLE);
    }

    /**
     * 点击"使用游币支付"
     */
    protected void onClickPayByKubi() {

    }

    /**
     * 设置标题
     * 
     * @param mTitle 标题
     */
    protected void setCommonTitle(final String title) {
        if (mTitle != null) {
            mTitle.setText(title);
        }
    }

    /**
     * 设置标题
     * 
     * @param strResId 字符串id
     */
    protected void setCommonTitle(int strResId) {
        if (mTitle != null) {
            mTitle.setText(strResId);
        }
    }

    /**
     * 回退键点击监听
     */
    private OnClickListener mBackBtnClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            hideSoftInput(mContext);
            onBackBtnClick();
        }
    };

    protected void onBackBtnClick() {
        onBackPressed();
    }

    public void setCommonRightImgListener(OnClickListener listener) {
        mCommonRightImg.setOnClickListener(listener);
    }

    protected void setCommonRightImageViewVisibility(int visibility) {
        mCommonRightImg.setVisibility(visibility);
    }

    protected void setCommonRightImgDrawable(Drawable drawable) {
        mCommonRightImg.setImageDrawable(drawable);
    }

    protected void setCommonRightImageResource(int resId) {
        mCommonRightImg.setImageResource(resId);
    }

    protected void setCommonLeftImageViewVisibility(int visibility) {
        mBackImg.setVisibility(visibility);
    }

    public void setFAQPageTitleStyle() {
        mHelpImage.setVisibility(View.GONE);
    }

    public void setHelpImageResource(int resId) {
        mHelpImage.setImageResource(resId);
    }

    public void setOnclickHelpImageListener(OnClickListener listener) {
        mHelpImage.setOnClickListener(listener);
    }

    public void setCommonTitleBarBackground(int color) {
        mTitleBar.setBackgroundResource(color);
    }

    private onShowGSImageListener onShowGSImageListener;

    public void setOnShowGSImageListener(onShowGSImageListener listener) {
        this.onShowGSImageListener = listener;
    }

    public interface onShowGSImageListener {
        public void onShow();
    }

    /**
     * 设置支付金额View的内容，如果没用到提示View的请不要使用
     * 
     * @param tipsText
     */
    protected void setCommonTipsBarMoneyNum(String tipsText) {
        if (mCommonTipsBarMoneyNum != null) {
            mCommonTipsBarMoneyNum.setText(tipsText);
        }
    }

    protected void setCommonTipsBarMoneyNum(int resId) {
        if (mCommonTipsBarMoneyNum != null) {
            mCommonTipsBarMoneyNum.setText(resId);
        }
    }

    /**
     * 设置支付金额View的内容(具有HTML格式)，如果没用到提示View的请不要使用
     * 
     * @param tipsText
     */
    protected void setCommonTipsBarMoneyNum(Spanned tipsText) {
        if (mCommonTipsBarMoneyNum != null) {
            mCommonTipsBarMoneyNum.setText(tipsText);
        }
    }

    protected void setCommonTipsBarMoneyNumVisiable(int visibility) {
        mCommonTipsBarMoneyNum.setVisibility(visibility);
    }

    public HelpMenuPopupWindow getHelpMenuPopupWindow() {
        return mHelpWindow;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        hideSoftInput(this);
    }

    @Override
    public void onBackPressed() {
        finish();
    }
}
